class Solution {
public:
    int calculate(string s) {
        stack<int> ret;
        char sign='+';
        for(int i=0;i<s.size();)
        {
            if(s[i]!=' ')
            {
                int tmp=0;
                while(i<s.size()&&s[i]>='0'&&s[i]<='9')
                {
                    tmp=tmp*10+(s[i]-'0');
                    i++;
                }
                if(sign=='+') 
                {
                    ret.push(tmp);
                }
                else if(sign=='-')
                {
                    ret.push(-tmp);
                }

                else if(sign=='*') 
                {
                    long b=ret.top();
                    b*=tmp;
                    ret.pop();
                    ret.push(b);
                }

                else if(sign=='/') 
                {
                    long c=ret.top();
                    c/=tmp;
                    ret.pop();
                    ret.push(c);
                }

            }
            else
            {
                i++;
            }
            if (i < s.size() &&
             (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')) {
                sign = s[i];
                i++;
            }
            
        }
        long a=0;
        while(!ret.empty())
        {
            a+=ret.top();
            ret.pop();
        }
        return a;
    }
};
